package com.burgeon.backend.EnterpriseWeChat.rest.VipInfo;

import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;


//vip信息
public class findVipInfo extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public findVipInfo(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<Vip> getData(String nameOrCardNo, int range) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(nameOrCardNo,range), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Vip> vipList = new ArrayList<>();

        if(jsonArray.length()>0){
            for (int i = 0; i < jsonArray.length(); i++) {
                Vip vip = new Vip();

                vip.setId(jsonArray.getJSONArray(i).getString(0));
                vip.setVipName(jsonArray.getJSONArray(i).getString(1));
                vip.setCardNo(jsonArray.getJSONArray(i).getString(2));
                if("null".equals(jsonArray.getJSONArray(i).getString(3))){
                    vip.setLogoImg("http://jxfs.cloudsmicro.com:8080/img/EnterpriseWeChat/vip/app.png");
                }else {
                    vip.setLogoImg(jsonArray.getJSONArray(i).getString(3));
                }
                vip.setOpenid(jsonArray.getJSONArray(i).getString(4));
                vip.setPhone(jsonArray.getJSONArray(i).getString(5));
                vip.setLastDate(jsonArray.getJSONArray(i).getString(6));
                vip.setModifiedDate(jsonArray.getJSONArray(i).getString(7));
                if("是".equals(jsonArray.getJSONArray(i).getString(8))){
                    vip.setUnread("Y");
                }else {
                    vip.setUnread("N");
                }
                vipList.add(vip);
            }

        }
        System.err.println(jsonArray.length());
        return vipList;
    }

    private MultiValueMap Data(String nameOrCardNo, int range) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("ID");
        colums.add("VIPNAME");//vip姓名
        colums.add("CARDNO");//卡号
        colums.add("LOGO_URL");//头像
        colums.add("OPENID2");
        colums.add("MOBIL");
        colums.add("'FA_VIPACC_ID;LASTDATE'");
        colums.add("'MODIFIEDDATE'");
        colums.add("UNREAD");//	UNREAD未读消息

        params.put("columns", colums);
        params.put("table", "C_VIP");//表名

        params.put("count",true);
        params.put("range",range);
        /*params.put("start",start);*/


        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();

        expr1.put("combine","or");
        expr1v1.put("column","VIPNAME");
        expr1v1.put("condition","'"+nameOrCardNo+"'");
        expr1.put("expr1",expr1v1);

        expr1v2.put("column","CARDNO");
        expr1v2.put("condition","'"+nameOrCardNo+"'");
        expr1.put("expr2",expr1v2);

        HashMap expr2 = new HashMap();

        expr2.put("column","ISACTIVE");
        expr2.put("condition","Y");

        paramss.put("combine","and");
        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);

        params.put("params", paramss);


        return DATA("Query", params, account, password);
    }


    public static void main(String[] args) throws Exception {
        findVipInfo g = new findVipInfo("39.104.240.134:55321", "nea@burgeon.com.cn", "root55321");
        System.err.println(g.getData("",35));
    }

}
